<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 搜索输入框 -->
      <div style="margin-top: 15px">
        <el-input placeholder="请输入内容" v-model="input2">
          <el-button
            @click="searchUsers"
            slot="append"
            icon="el-icon-search"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="addUsers">添加用户</el-button>
      </div>
      <!-- 用户数据列表 -->
      <el-table
        ref="singleTable"
        :data="list"
        border
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column label="序号" type="index" width="50">
          <template slot-scope="{ $index }">
            {{ obj.pagesize * (obj.pagenum - 1) + $index + 1 }}
          </template>
        </el-table-column>
        <el-table-column property="username" label="姓名" width="200">
        </el-table-column>
        <el-table-column property="email" label="邮箱" width="220">
        </el-table-column>
        <el-table-column property="mobile" label="电话"> </el-table-column>
        <el-table-column property="role_name" label="角色"> </el-table-column>
        <el-table-column property="mg_state" label="状态">
          <template slot-scope="scope">
            <!-- {{scope.row}} -->
            <el-switch
              @change="handlechange(scope.row)"
              v-model="scope.row.mg_state"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button
              @click="editHandle(row)"
              type="primary"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button
              @click="del(row.id)"
              type="danger"
              icon="el-icon-delete"
              size="mini"
            ></el-button>
            <el-tooltip content="分配角色" placement="top">
              <el-button
                type="warning"
                icon="el-icon-star-off"
                size="mini"
                @click="fenpei(row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="obj.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
      <!-- 编辑对话框 -->
      <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
        <el-form :model="editForm" :rules="rules" ref="editForm">
          <el-form-item label="用户名" :label-width="formLabelWidth">
            <el-input
              disabled
              v-model="editForm.role_name"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model="editForm.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="手机"
            :label-width="formLabelWidth"
            prop="mobile"
          >
            <el-input v-model="editForm.mobile" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="submit1">确定</el-button>
        </div>
      </el-dialog>
      <!-- 添加用户对话框 -->
      <el-dialog title="修改用户" :visible.sync="dialogFormVisible1">
        <el-form :model="addForm" :rules="rules1" ref="addForm">
          <el-form-item
            label="用户名"
            :label-width="formLabelWidth"
            prop="username"
          >
            <el-input v-model="addForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            :label-width="formLabelWidth"
            prop="password"
          >
            <el-input v-model="addForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="邮箱" :label-width="formLabelWidth" prop="email">
            <el-input v-model="addForm.email" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="手机"
            :label-width="formLabelWidth"
            prop="mobile"
          >
            <el-input v-model="addForm.mobile" autocomplete="off"></el-input>
          </el-form-item>
          {{ addForm }}
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible1 = false">取 消</el-button>
          <el-button type="primary" @click="submit2">确定</el-button>
        </div>
      </el-dialog>
      <!-- 分配角色对话框 -->
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible2">
        <el-form :model="form">
          <el-form-item label="当前的用户:" :label-width="formLabelWidth">
            {{ editForm.username }}
          </el-form-item>
          <el-form-item label="当前的角色:" :label-width="formLabelWidth">
            {{ editForm.role_name }}
          </el-form-item>
          <el-form-item label="分配新角色" :label-width="formLabelWidth">
            <el-select v-model="form.region" placeholder="请选择">
              <el-option label="管理员" value="shanghai"></el-option>
              <el-option label="wqare" value="beijing"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible2 = false">取 消</el-button>
          <el-button type="primary" @click="btnFenpei">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>
<script>
import { getUsersAPI, editUsers, delUsers, addUsersAPI, PutApi } from '@/api/users'
import { getToken } from '@/utils/storage'
export default {
  name: '',
  data () {
    return {
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      value: true,
      value1: true,
      input2: '',
      list: [],
      total: 0,
      currentPage: 4,
      obj: {
        query: '',
        pagenum: 1,
        pagesize: 1,
      },
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      formLabelWidth: '120px',
      Form: {},
      editForm: {
        id: '',
        email: '',
        mobile: ''
      },
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      rules: {
        email: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      },
      rules1: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '电话号码不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  created () {
    this.getData()
    // const res1 = getToken()
    // console.log(res1, 'token');
  },
  computed: {
  },
  methods: {
    async getData () {
      const res = await getUsersAPI(this.obj)
      console.log(res, 666);
      this.list = res.data.users
      this.total = res.data.total
    },
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`);
      this.obj.pagesize = val
      this.getData()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`);
      this.obj.pagenum = val
      this.getData()
    },
     async handlechange (info) {
      console.log(info.id, 8787878)
      console.log(typeof info.mg_state, 8787878333)
      // this.getUserInfo()
      const res = await PutApi(info.id, info.mg_state)
      console.log(res, 656565)

      if (res.meta.status !== 200) {
        info.mg_state = !info.mg_state
        return this.$message.error('用户信息更新失败')
      }
      this.$message.success('用户信息更新成功')
    },
    editHandle (row) {
      console.log(row);
      this.dialogFormVisible = true
      this.editForm = JSON.parse(JSON.stringify(row))
      // console.log(this.editForm);
    },
    async submit1 () {
      const res = await editUsers({ email: this.editForm.email, mobile: this.editForm.mobile }, this.editForm.id)
      console.log(res, 888);
      this.getData()
      this.dialogFormVisible = false
    },
    del (id) {
      // await ArticleDelApi(id)
      // this.$message.success('删除数据成功')

      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        //b表示点击的是确定按钮
        await delUsers(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        this.getData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addUsers () {
      this.dialogFormVisible1 = true
    },
    async submit2 () {
      console.log(8899);
      console.log(this.addForm, 'addForm');
      const res = await addUsersAPI(this.addForm)
      // console.log(res);
      this.getData()
      this.dialogFormVisible1 = false
    },
    async searchUsers () {
      console.log(this.input2);
      this.obj.query = this.input2
      this.obj.pagenum = 1
      const res = await getUsersAPI(this.obj)
      console.log(res, 99999);
      this.getData()
    },
    fenpei (row) {
      console.log(row);
      this.dialogFormVisible2 = true
      this.editForm = JSON.parse(JSON.stringify(row))
    },
    btnFenpei () {
      console.log(this.form.region);
      // this.dialogFormVisible2 = false
      if (this.form.region == '') {
        this.$message({
          type: 'error',
          message: '请选择要分配的角色!'
        });
      } else {
        this.$message({
          type: 'error',
          message: '更新角色失败!'
        });
      }
    }
  }
}
</script>
<style lang='less'  scoped>
.el-form {
  width: 90%;
}
.box-card {
  margin-top: 15px;
  .el-input {
    width: 500px;
    margin-right: 20px;
  }
}
</style>
